﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Face.Web.Admin
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        private static IBLL.IVideoInAdmin bll = BLLFactory.DataAccess.GetVideoInfo();
        private int CurrentPage = 1;
        private int TotalPage = 1;
        private int sortCode = 1;
        public string showName;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                bindData();
                Cache.Remove("CurrentPage");
                Cache.Insert("CurrentPage", CurrentPage);
                Cache.Remove("sortCode");
                Cache.Insert("sortCode", sortCode);
            }
            else
            {
                CurrentPage = (int)Cache["CurrentPage"];
                sortCode = (int)Cache["sortCode"];
            }
            showName=Session["admin"].ToString();
        }

        private void bindData()
        {
            //调用业务层
            if (sortCode == 1)
                Video_info.DataSource = bll.GetOnePageVideo(CurrentPage);
            else
                Video_info.DataSource = bll.GetOnePageVideoByTime(CurrentPage);

            Video_info.DataBind();
        }

        public void LastPage(object sender, EventArgs e)
        {

            if (CurrentPage > 1)
            {
                CurrentPage--;
                Cache.Insert("CurrentPage", CurrentPage);
                bindData();
            }
        }

        public void NextPage(object sender, EventArgs e)
        {
            if (bll.GetVideoCount() % 6 != 0)
            {
                TotalPage = bll.GetVideoCount() / 6 + 1;
            }
            else
            {
                TotalPage = bll.GetVideoCount() / 6 ;
            }
            if (CurrentPage < TotalPage)
            {
                CurrentPage++;
                Cache.Insert("CurrentPage", CurrentPage);
                bindData();
            }
        }

        protected void Button_Clicked(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "CheckVideo")
            {

            }
            else if (e.CommandName == "DeleteVideo")
            {
                bll.DeleteVideoById(System.Int32.Parse(e.CommandArgument.ToString()));
                bindData();
            }
        }
        protected void SearchUser(object sender, EventArgs e)
        {
            Video_info.DataSource = bll.GetVideoByUser(searchBox.Value);
            Video_info.DataBind();
        }

        protected void SortByName(object sender, EventArgs e)
        {
            CurrentPage = 1;
            sortCode = 1;
            Cache.Insert("sortCode", sortCode);
            bindData();
        }

        protected void SortByTime(object sender, EventArgs e)
        {
            CurrentPage = 1;
            sortCode = 2;
            Cache.Insert("sortCode", sortCode);
            bindData();
        }


        protected void btn_logout(object sender, EventArgs e)
        {
            Session.Remove("admin");
            Response.Redirect("/Admin/login.aspx");
        }
    }
}